﻿@{
    string connString =  System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(connString);
}

<!DOCTYPE html>

<html lang="pl">
    <head>
        <meta charset="utf-8" />
        <title>Layout</title>
        <link href="Images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <link rel="stylesheet" type="text/css" href="Shared/styles.css">
    </head>

    <body>
        <!-- Header -->
        <p style="font-family: 'Segoe UI'; font-size: 45px; color: #fff; padding: 0px; margin: 0px 25px 0px 10px; float: left">Serwery</p>
        <a href="Subnets.cshtml"><p style="font-family: 'Segoe UI'; font-size: 45px; color: #808080; padding: 0px; margin: 0px 25px 0px 10px; float: left">Podsieci</p></a>
        <a href="Hosts.cshtml"><p style="font-family: 'Segoe UI'; font-size: 45px; color: #808080; padding: 0px; margin: 0px 25px 0px 10px; float: left">Stacje robocze</p></a>
        <a href="Config.cshtml"><p style="font-family: 'Segoe UI'; font-size: 45px; color: #808080; padding: 0px; margin: 0px 25px 0px 10px">System</p></a>
        <!-- Content -->
            <!-- Add -->
            <div style="width: 450px; height: auto; position: absolute; margin-left: 10px; margin-top: 10px">
                <p style="font-family: 'Segoe UI'; font-size: 20px; color: #808080; margin: 0px 0px 20px 0px">Dodaj serwer</p>

                <form action="Add.cshtml" style="color: #fff; font-family: 'Segoe UI'" method="post">
                    <a style="width: 205px; display: inline-block; margin-bottom: 7px">Nazwa:</a><input style="width: 230px" type="text" name="name"><br>
                    <a style="width: 205px; display: inline-block; margin-bottom: 7px">DNS:</a><input style="width: 230px" type="text" name="domain_name_servers"><br>
                    <a style="width: 205px; display: inline-block; margin-bottom: 7px">Domyślny czas dzierżawy:</a><input style="width: 230px" type="text" name="default_lease_time"><br>
                    <a style="width: 205px; display: inline-block; margin-bottom: 7px">Maksymalny czas dzierżawy:</a><input style="width: 230px" type="text" name="max_lease_time"><br>
                    <a style="width: 205px; display: inline-block; margin-bottom: 7px">Autorytatywny:</a><input style="width: 230px" type="text" name="authoritative"><br>
                    <a style="width: 205px; display: inline-block; margin-bottom: 7px">Log obiektu:</a><input style="width: 230px" type="text" name="log_facility"><br>
                    <input type="hidden" value="servers" name="from">
                    <input style="width: 100px; margin-top: 8px; margin-left: 339px" type="submit" value="Dodaj">
                </form>
            </div>
            <!-- List -->
            <div style="width: auto; height: auto; position: absolute; left: 460px; margin-top: 10px">
                <p style="font-family: 'Segoe UI'; font-size: 20px; color: #808080; margin: 0px 0px 20px 3px">Lista wszystkich serwerów</p>
                <div style="overflow-x: auto; overflow-y: auto; overflow: scroll; height: 750px">
                    <!-- Tiles -->
                    @{
                        // Pobranie danych
                        MySql.Data.MySqlClient.MySqlCommand command = new MySql.Data.MySqlClient.MySqlCommand("SELECT id, name FROM servers;", connection);
                        connection.Open();

                        MySql.Data.MySqlClient.MySqlDataReader reader = command.ExecuteReader();

                        System.Data.DataTable dt = new System.Data.DataTable();
                        dt.Load(reader);
                        
                        reader.Close();
                        connection.Close();
                        //Konice pobierania danych

                        foreach(System.Data.DataRow row in dt.Rows)
                        {
                            connection.Open();
                            System.Data.IDataRecord record = (System.Data.IDataRecord)reader;
                            string name = row[1].ToString();
                            string id = row[0].ToString();
                            string url = "Selected.cshtml?selected="+name+"&from=servers";
                            connection.Close();
                            reader.Close();

                            int count = 0;

                            // Pobieranie liczby
                            try
                            {
                                connection.Open();
                                MySql.Data.MySqlClient.MySqlCommand com = new MySql.Data.MySqlClient.MySqlCommand("SELECT COUNT(id) FROM subnets WHERE server="+id+";", connection);
                                count = Convert.ToInt32(com.ExecuteScalar());
                            }
                            catch(Exception ex)
                            {
                                Response.Write(name);
                                Response.Write(id);
                                Response.Write(count);

                                Response.Write(ex.Message);
                            }
                            finally
                            {
                                connection.Close();
                            }

                            if(name.Length <= 12)
                            {
                                <a href="@url">
                                    <div class="tile">
                                        <span>@count</span>
                                        <p>@name</p>
                                    </div>
                                </a>
                            }
                            else
                            {
                                string tmp = name.Substring(0,12)+"...";
                                <a href="@url">
                                    <div class="tile">
                                        <span>@count</span>
                                        <p>@tmp</p>
                                    </div>
                                </a>
                            }
                        }
                        
                    }
                </div>
            </div>
        <!-- Menu -->
        <div style="height: 30px; width: 100%; background-color: #0094ff; position: fixed; bottom: 0px; padding-left: 10px">
            <a href="Search.cshtml?from=servers"><div class="menu_item"><p style="color: #fff; margin: 3px; font-family: 'Segoe UI'">Wyszukaj</p></div></a>
        </div>
    </body>
</html>